﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace GameToolkit.Editor
{

    [System.Serializable]
    public class StoryGraphSelection : ScriptableObject, IReferencedAssetEditor
    {
        public StoryGraph currentGraph { get; private set; }

        [SerializeField]
        bool m_WasSelected;

        [SerializeField]
        TaskGraphAsset m_GraphAsset;

        [SerializeReference]
        internal List<TaskGraphAsset.NodeAsset> m_GraphNodes = new List<TaskGraphAsset.NodeAsset>();
        public Object referencedTarget => m_GraphAsset;


        public void GetSelections(StoryGraph graph)
        {
            currentGraph = graph;
            m_GraphAsset = graph.asset;
            m_GraphNodes.Clear();
            foreach (var sel in graph.selection)
            {
                if (sel is IStoryNodeVisual visual)
                {
                    var asset = visual.nodeAsset;
                    m_GraphNodes.Add(asset);
                }
            }
            EditorUtility.SetDirty(this);
            if (m_GraphNodes.Count > 0 || m_WasSelected)
            {
                Selection.activeObject = this;
            }
            m_WasSelected = m_GraphNodes.Count > 0;
            //else
            //{
            //    Selection.activeObject = graph.asset;
            //}

        }
    }
}
